//
//  ExercieseQuestionReadingViewController.m
//  ShowYork
//
//  Created by hxh on 2020/9/8.
//  Copyright © 2020 hxh. All rights reserved.
//


#define MAXY 300
#define MINY 100
#define ITEMH 44

#import "ExercieseQuestionReadingViewController.h"
#import "ExercisesItemTitleViewController.h"
#import "ExercisesQuestionChoiceViewController.h"
#import "ExercieseQuestionSubjectiveViewController.h"
#import "ExercieseQuestionReadingCell.h"
@interface ExercieseQuestionReadingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIScrollView *panView;
@property (nonatomic,strong)UIView *bottomCoverView;
@property (nonatomic,strong)UIView *bottomBar;
@property (nonatomic, strong) GenericPanGestureHandler *bottomBarGestureHandler;
@end

@implementation ExercieseQuestionReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
//    
//    self.view.backgroundColor = RGBA(248, 248, 248, 1);
      
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.panView];
    [self.view addSubview:self.bottomCoverView];
    
    
    self.bottomCoverView.frame = CGRectMake(0,kScreenH - MAXY, kScreenW, ITEMH);

    self.tableView.frame = CGRectMake(0, 0, kScreenW, kScreenH - MAXY);
    
    self.panView.frame = CGRectMake(0,0,kScreenW, kScreenH - MAXY);
    
    self.panView.cl_y = self.bottomCoverView.cl_bottom;

    __weak typeof(self) weakSelf = self;
    self.bottomBarGestureHandler = [[GenericPanGestureHandler alloc] initWithGestureView:self.bottomCoverView handleView:self.bottomCoverView minY:ITEMH maxY:kScreenH - MAXY];
    self.bottomBarGestureHandler.offsetChangeBlock = ^(CGFloat afterY, CGFloat offsetY, BOOL direction) {
    
        weakSelf.panView.cl_y = weakSelf.bottomCoverView.cl_bottom;
        
        weakSelf.tableView.cl_height = weakSelf.bottomCoverView.cl_bottom - ITEMH;
    
    };
    self.bottomBarGestureHandler.shouldResponseGesture = ^BOOL{
        return YES;
    };

    
    for (int i = 0; i < 3 ; i++) {
        if (i == 0) {
            ExercisesItemTitleViewController *vc = [[ExercisesItemTitleViewController alloc]init];
            [self.panView addSubview:vc.view];
            [self addChildViewController:vc];
            
            vc.view.frame = CGRectMake(kScreenW * 0, 0, kScreenW, self.panView.cl_height);
            
        }
        if (i == 1) {
            ExercisesQuestionChoiceViewController *vc = [[ExercisesQuestionChoiceViewController alloc]init];
            [self.panView addSubview:vc.view];
            [self addChildViewController:vc];
            vc.view.frame = CGRectMake(kScreenW * 1, 0, kScreenW, self.panView.cl_height);
        }
        if (i == 2) {
            ExercieseQuestionSubjectiveViewController *vc = [[ExercieseQuestionSubjectiveViewController alloc]init];
            [self.panView addSubview:vc.view];
            [self addChildViewController:vc];
             vc.view.frame = CGRectMake(kScreenW * 2, 0, kScreenW, self.panView.cl_height);
        }
    }
}


- (UIScrollView *)panView{
    if (!_panView) {
        _panView = [[UIScrollView alloc]init];
        _panView.pagingEnabled = YES;
        _panView.contentSize = CGSizeMake(kScreenW * 3, 0);
    }
    return _panView;
}

- (UIView *)bottomBar{
    if (!_bottomBar) {
        _bottomBar = [[UIView alloc]init];
        _bottomBar.backgroundColor = RGBA(216, 216, 216, 1);
        _bottomBar.layer.cornerRadius = 2.5;
        _bottomBar.layer.masksToBounds = YES;
        
    }
    return _bottomBar;
}
- (UIView *)bottomCoverView{
    if (!_bottomCoverView) {
        _bottomCoverView = [[UIView alloc]init];
        [_bottomCoverView addSubview:self.bottomBar];
        _bottomBar.cl_size = CGSizeMake(36, 5);
        _bottomBar.cl_x = (kScreenW - 36)/2;
        _bottomBar.cl_y = 16;
    }
    return _bottomCoverView;
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 100;
        _tableView.estimatedSectionFooterHeight = .0f;
        _tableView.estimatedSectionHeaderHeight = .0f;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExercieseQuestionReadingCell *cell = [ExercieseQuestionReadingCell cellWithTableView:tableView];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}



@end
